package com.yugao.fintech.draper.admin.controller;

import com.yugao.fintech.draper.admin.param.UserInfo;
import com.yugao.fintech.draper.admin.service.UserService;
import com.yugao.fintech.draper.core.model.ResultVO;
import com.yugao.fintech.draper.security.annotation.SecurityInner;
import com.yugao.fintech.draper.security.SecurityUser;
import com.yugao.fintech.draper.security.util.SecurityUserUtil;
import com.yugao.fintech.draper.core.exception.BaseException;
import com.yugao.fintech.draper.datasource.entity.PageResult;
import com.yugao.fintech.draper.auth.dto.UserInfoVO;
import com.yugao.fintech.draper.admin.param.UserQuery;
import com.yugao.fintech.draper.tenant.aspect.IgnoreTenant;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.constraints.NotBlank;

@Slf4j
@Validated
@RestController
@RequestMapping("/user")
@RequiredArgsConstructor
@Tag(name = "系统用户管理")
public class UserController {

	private final UserService userService;

	@Operation(summary = "查询用户信息分页列表")
	@GetMapping
	@PreAuthorize("@ps.hasPermission('sys:user:view')")
	public ResultVO<PageResult<UserInfoVO>> pageQuery(UserQuery userPageDTO) {
		return ResultVO.success(PageResult.success(userService.pageQuery(userPageDTO)));
	}

	@Operation(summary = "根据id获取用户信息")
	@GetMapping("/findUserById/{id}")
	public ResultVO<UserInfoVO> findUserById(@PathVariable("id") Long id) {
		return ResultVO.success(userService.findUserById(id));
	}

	@Operation(summary = "添加用户")
	@PostMapping
	@PreAuthorize("@ps.hasPermission('sys:user:add')")
	public void addUser(@RequestBody UserInfoVO user) {
		this.userService.createUser(user);
	}

	@Operation(summary = "更新用户")
	@PutMapping
	@PreAuthorize("@ps.hasPermission('sys:user:update')")
	public void updateUser(@RequestBody UserInfoVO user) {
		this.userService.updateUser(user);
	}

	@Operation(summary = "修改用户基础信息")
	@PutMapping("/info")
	@PreAuthorize("@ps.hasPermission('sys:user:update')")
	public void updateUserInfo(@RequestBody UserInfoVO user) {
		this.userService.updateUserInfo(user);
	}

	@Operation(summary = "删除用户")
	@DeleteMapping("/{userIds}")
	@PreAuthorize("@ps.hasPermission('sys:user:delete')")
	public void deleteByUserIds(@NotBlank(message = "{required}") @PathVariable String userIds) throws BaseException {
		String[] ids = userIds.split(StringPool.COMMA);
		this.userService.deleteByUserIds(ids);
	}

	@Operation(summary = "通过用户名查找用户信息")
	@SecurityInner
	@IgnoreTenant
	@GetMapping("/findByName/{username}")
	public ResultVO<UserInfoVO> findByName(@PathVariable("username") String username) {
		return ResultVO.success(this.userService.findByUserName(username));
	}

	@Operation(summary = "通过手机号查找用户信息")
	@SecurityInner(inner = true)
	@GetMapping("/findByMobile/{mobile}")
	public ResultVO<UserInfoVO> findByMobile(@PathVariable("mobile") String mobile) {
		return ResultVO.success(this.userService.findByMobile(mobile));
	}

	@Operation(summary = "获取当前用户全部信息")
	@GetMapping("/info")
	public ResultVO<UserInfo> userInfo() {
		SecurityUser user = SecurityUserUtil.getCurrentUser();
		// 查询用户信息
		UserInfoVO userDTO = userService.findUserById(user.getUserId());
		return ResultVO.success(userService.findUserInfo(userDTO));
	}
}